package model;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import architecture.ArchitecturePackage;
import architecture.Responsability;
import architecture.impl.ArchitectureImpl;
import architecture.impl.ModuleImpl;

public class EMFGraphReader implements IGraphReader {

	private String src = null;
	private ArchitectureImpl arch = null;
	private Iterator<architecture.Module> modules = null;
	HashMap<String,architecture.Module> hashModules = null;

	public model.Module getModule() {

		if(modules != null && modules.hasNext()){

			ModuleImpl m = (ModuleImpl) modules.next();			
			model.Module gModule = new model.Module();
			gModule.setComplexity(m.getComplexity());
			gModule.setName(m.getName());
			EList<Responsability> resps = m.getImplement();
			for(int i = 0; i < resps.size(); i++){
				gModule.addResponsability(resps.get(i).getName());
			}
			return gModule;
		}
		else return null;
	}

	public Vector<String> getRelationships(String aSrcModule) {

		if(hashModules.containsKey(aSrcModule) && hashModules.get(aSrcModule).getDepend() != null){
			Vector<String> relations = new Vector<String>();
			Iterator<architecture.Module> rs = hashModules.get(aSrcModule).getDepend().iterator();

			while(rs.hasNext()){
				architecture.Module im = rs.next();
				relations.add(im.getName());
			}

			return relations;
		}else{
			return null;
		}
	}

	public void setSrc(String srcName) throws IOException {
		this.src = srcName;
		this.loadResource();

	}

	private void loadResource() throws IOException{
		System.out.println("DEBUG: EMFGraphReader - start loadResurces");
		ArchitecturePackage.eINSTANCE.eClass();
		Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
		Map<String, Object> m = reg.getExtensionToFactoryMap();
		m.put("architecture", new XMIResourceFactoryImpl());
		ResourceSet resSet = new ResourceSetImpl();		
		resSet.getLoadOptions().put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, Boolean.TRUE);
		Resource resource = resSet.getResource(URI.createFileURI(this.src), true);

		arch = (ArchitectureImpl)resource.getContents().get(0);

		modules = arch.getHasModules().iterator();
		hashModules = new HashMap<String,architecture.Module>(); 

		Iterator<architecture.Module> itModules= arch.getHasModules().iterator();
		while(itModules.hasNext()){
			architecture.Module im = itModules.next();
			hashModules.put(im.getName(),im);
		}
		System.out.println("DEBUG: EMFGraphReader - end loadResurces");

	}

	/*
	public static void main(String[] args) {

		EMFGraphReader reader = new EMFGraphReader();
		System.out.println(reader.getClass().getName());
	//	reader.setSrc("C:\\Documents and Settings\\Sol\\Escritorio\\default.architecture");
	}*/
}